<?php

App::uses('AppController', 'Controller');
App::uses('MyEmail', 'Lib');

/**
 * Users Controller
 *
 */
class UsersController extends AppController {

    /**
     * Scaffold
     *
     * @var mixed
     */
    public $scaffold;
    public $uses = array('UserGroup', 'User');

    public function login() {
        if ($this->request->is('post')) {
            $this->User->set($this->request->data['User']);
            $validator = $this->User->validator();
            unset($validator['email']['dupplicate']);
            unset($validator['password']['passwordLength']);
            if ($this->User->validates(array('fieldList' => array('email', 'password')))) {
                $email = $this->request->data['User']['email'];
                $password = $this->request->data['User']['password'];
                $data = $this->User->val($email, $password);
                //debug($data)
                if ($data != NULL) {
                    $this->Session->write('User.id', $data['User']['id']);
                    $this->Session->write('User.email', $data['User']['email']);
                    $this->Session->write('User.first_name', $data['User']['first_name']);
                    $this->Session->write('User.last_name', $data['User']['last_name']);
                    $this->Session->write('User.username', $data['User']['username']);
                    $this->redirect('/Admins');
                }
            }
        }
    }

    public function signout() {
        $this->autoRender = false;
        //$this->Session->delete('User');
        SessionComponent::destroy();
        $this->redirect('/');
    }

    public function index() {
        $data = $this->User->find('all');
        $this->set('users', $data);
    }

    public function add() {
        if ($this->request->is('post')) {
            $utility = new Utility();
            $email = new MyEmail();

            $your_first_name = $this->request->data['User']['first_name'];
            $your_last_name = $this->request->data['User']['last_name'];
            $your_email = $this->request->data['User']['email'];
            $emailEncode = $utility->base64url_encode($your_email);
            $keyCode = $emailEncode . Configure::read('Security.cipherSeed');
            $password = $utility->generate_random_password(20);

            $this->request->data['User']['username'] = $your_first_name . '.' . strtolower($your_last_name[0]);
            $this->request->data['User']['key_code'] = $keyCode;
            $this->request->data['User']['password'] = $password;
            $this->User->set($this->request->data['User']);
            //debug($this->request->data);
            if ($this->User->validates()) {
                $this->User->save();
                $this->Session->setFlash('Add Success!!');
                $url = Router::url('/', true);
                $url = $url . 'Users/activate?e=' . $emailEncode . '&ss=' . $keyCode;

                $email->avtivateEmail($your_email, $your_first_name . '   ' . $your_last_name, $url, $password);

                $this->redirect('/Admins');
            }
        }
        $data = $this->UserGroup->listOptionToDropdown();
        $this->set('userGroup', $data);
    }

    public function activate() {
        $this->autoRender = false;
        $utility = new Utility();
        $keyCode = $this->params['url']['ss'];
        $email = $utility->base64url_decode($this->params['url']['e']);

        $data = $this->User->find('first', array(
            'conditions' => array(
                'email' => $email,
                'key_code' => $keyCode,
                'activate' => 'no'
            )
        ));
        if ($data == NULL) {
            $this->redirect('/');
        }
        if ($data) {
            $this->User->id = $data['User']['id'];
            $this->User->saveField('activate', 'yes');
        }
        $this->Session->setFlash('Account successfully activated.', 'default', array('class' => 'font-green'));

        $this->Session->write('User.id', $data['User']['id']);
        $this->Session->write('User.email', $data['User']['email']);
        $this->Session->write('User.first_name', $data['User']['first_name']);
        $this->Session->write('User.last_name', $data['User']['last_name']);
        $this->Session->write('User.username', $data['User']['username']);

        $this->redirect('/Users/change_password/' . $data['User']['id'] . '?kc=' . $keyCode);
    }

    public function change_password($user_id) {
        if ($this->request->is('post')) {
            $this->User->set($this->request->data);
            if ($this->User->validates(array('fieldList' => array('password', 'confirm_password', 'old_password')))) {
                $old_password = $this->request->data('User.old_password');
                $data = $this->User->Find('first', array(
                    'conditions' => array('User.id' => $user_id,
                        'password' => md5($old_password))
                ));
                if ($data) {
                    $this->User->id = $data['User']['id'];
                    $this->User->saveField('password', md5($this->request->data('User.password')));
                    $this->User->saveField('change_password', 'Y');
                    $this->Session->write('User.change_password', 'Y');
                    $this->Session->write('Msg.success', __('change password success'));
                    $this->redirect('/');
                } else {
                    $this->Session->write('Msg.error', __('old_password_incorrect'));
                }
            }
            unset($this->request->data['User']);
        } else {
            $data = $this->User->Find('first', array(
                'conditions' => array(
                    'User.id' => $user_id
                )
            ));

            $this->set('user', $data);
        }
    }

}